package com.jinzihan.www.view;

import com.jinzihan.www.dao.CustomerDaoImpl;
import com.jinzihan.www.dao.MerchantDaoImpl;
import com.jinzihan.www.entity.*;
import com.jinzihan.www.service.CommodityService;
import com.jinzihan.www.util.CMUtility;

import java.util.Scanner;


public class View {
    public void enterMainMenu() {
        while (true) {
            //创建键盘录入对象，获取键盘录入信息
            Scanner sc = new Scanner(System.in);
            System.out.println("------------------------欢迎来到注册登录界面！------------------------");
            System.out.println("1.商家注册");
            System.out.println("2.买家注册");
            System.out.println("3.商家登录");
            System.out.println("4.买家登录");
            System.out.println("5.退出");
            String choice = sc.nextLine();

            //创建一个用户操作类
            MerchantDaoImpl MerchantUdi = new MerchantDaoImpl();
            CustomerDaoImpl CustomerUdi = new CustomerDaoImpl();

            switch (choice) {
                case "1" -> {
                    System.out.println("----------------------欢迎来到商家注册界面！----------------------");
                    System.out.println("请输入用户名(不超过十个字)：");
                    String merchantAccount = CMUtility.readString(10);
                    System.out.println("请输入密码(不超过十五个字)：");
                    String merchantPassword = CMUtility.readString(15);
                    System.out.println("请输入您的昵称(不超过八个字)：");
                    String merchantName = CMUtility.readString(8);
                    Merchant merchant = new Merchant(merchantAccount, merchantPassword ,merchantName);
                    MerchantUdi.regist(merchant);
                    System.out.println("----------------------------注册成功！----------------------------");
                }
                case "2" -> {
                    System.out.println("----------------------欢迎来到买家注册界面！----------------------");
                    System.out.println("请输入用户名(不超过十个字)：");
                    String customerName = CMUtility.readString(10);
                    System.out.println("请输入密码(不超过十五个字)：");
                    String CustomerPassWord = CMUtility.readString(15);
                    System.out.println("请输入您的昵称(不超过八个字)：");
                    String Name = CMUtility.readString(8);
                    Customer customer = new Customer(customerName, CustomerPassWord,Name,0.0);
                    CustomerUdi.regist(customer);
                    System.out.println("----------------------------注册成功！----------------------------！");
                }
                case "3" -> {
                    System.out.println("----------------------欢迎来到商家登录界面！----------------------");
                    System.out.println("请输入用户名：");
                    String inputMerchantName = CMUtility.readString(10);
                    System.out.println("请输入密码：");
                    String InputMerchantPassWord = CMUtility.readString(15);
                    if (MerchantUdi.isLogin(inputMerchantName, InputMerchantPassWord)) {
                        System.out.println("-------------------------登陆成功!---------------------------");
                    } else {
                        System.out.println("-------------------------登录失败！---------------------------");
                        break;
                    }
                        MerchantView merchantView = new MerchantView();
                        merchantView.enterMainMenu();
                }
                case "4" -> {
                    System.out.println("----------------------欢迎来到买家登录界面！----------------------");
                    System.out.println("请输入用户名：");
                    String inputCustomerName = CMUtility.readString(10);
                    System.out.println("请输入密码：");
                    String InputCustomerPassWord = CMUtility.readString(15);
                    if (CustomerUdi.isLogin(inputCustomerName, InputCustomerPassWord)) {
                        System.out.println("登陆成功！");
                    } else {
                        System.out.println("登录失败！");
                        break;
                    }
                    CustomerView customerView = new CustomerView();
                    customerView.enterMainMenu(inputCustomerName , InputCustomerPassWord);
                }
                case "5"->
                        System.exit(0);
                default -> System.out.println("输入格式不对，请重新输入");
            }
        }
    }

    public static class CustomerView {

        final CustomerDaoImpl customerDao = new CustomerDaoImpl();
        private final CommodityService commodityList =new CommodityService();
        public void enterMainMenu(String inputCustomerName ,String InputCustomerPassWord){
            Customer customer = customerDao.getCustomer(inputCustomerName,InputCustomerPassWord);
            boolean ifFlag=true;
            while(ifFlag){
                System.out.println("----------------------顾客界面-------------------------");
                System.out.println(customer.getName()+"你好");
                System.out.println("                    1.查看商品");
                System.out.println("                    2.查看余额");
                System.out.println("                    3.查看背包");
                System.out.println("                    4.充值");
                System.out.println("                    5.退出\n");
                System.out.print("                 请您选择（1-5）:");
                char menu= CMUtility.readMenuSelection();
                switch (menu) {
                    case '1' -> listAllCommodity(inputCustomerName,InputCustomerPassWord);
                    case '2' -> checkBalance(inputCustomerName,InputCustomerPassWord);
                    case '3' -> checkBag(inputCustomerName,InputCustomerPassWord);
                    case '4' -> pay(inputCustomerName, InputCustomerPassWord);
                    case '5' -> {
                        System.out.println("是否确认退出（Y/N）");
                        char isExit = CMUtility.readConfirmSelection();
                        if (isExit == 'Y') {
                            ifFlag = false;
                        }
                    }
                }
            }
        }

        private void checkBalance(String inputCustomerName ,String InputCustomerPassWord){
            Customer customer = customerDao.getCustomer(inputCustomerName,InputCustomerPassWord);
            System.out.println("您的余额为"+customer.getWealth());
        }

        private void pay(String inputCustomerName ,String InputCustomerPassWord) {
            Customer customer = customerDao.getCustomer(inputCustomerName,InputCustomerPassWord);
            System.out.println("--------------------------充值入口--------------------------");
            System.out.println("10软妹币 ----->  1000000￥");
            System.out.println("请输入您充值的金额（软妹币）：");
            Scanner sc = new Scanner(System.in);
            double money = sc.nextDouble();
            System.out.println("是否确认充值（Y/N）");
            char isExit = CMUtility.readConfirmSelection();
            if (isExit == 'Y') {
                customer.setWealth(customer.getWealth()+100000*money);
                System.out.println("充值成功");
                System.out.println("您当前的余额为"+customer.getWealth());
            }else {
                System.out.println("充值失败");
            }

        }

        private void checkBag(String inputCustomerName ,String InputCustomerPassWord){
            Customer customer = customerDao.getCustomer(inputCustomerName,InputCustomerPassWord);
            Node tmp = customer.getBag();
            while (tmp != null) {
                System.out.print(tmp.commodity.getBagInfo()+"\n");
                tmp = tmp.next;
            }
        }

        private void listAllCommodity(String inputCustomerName ,String InputCustomerPassWord) {
            Customer customer = customerDao.getCustomer(inputCustomerName,InputCustomerPassWord);
            CommodityService.printAllCommodity();
            System.out.println("1-购买秘籍\t2-退出");
            int choice = CMUtility.readInt();
            switch (choice){
                case 1 :{
                    System.out.println("请选择要购买的商品的编号（输入-1退出）：");
                    Node node;
                    int number;
                    for (; ; ) {
                        number = CMUtility.readInt();
                        if (number == -1) {
                            return;
                        }
                        node = commodityList.searchNodeLocation(number - 1);
                        if (node == null) {
                            System.out.println("无法找到指定秘籍！");
                        } else {
                            break;
                        }
                    }
                    if(customer.getWealth()>=node.commodity.getPrice()) {
                        node.commodity.setStatus(Status.sold_out);
                        customer.setWealth(customer.getWealth() - node.commodity.getPrice());
                        if (customer.getBag() == null) {
                            customer.setBag(node);
                        } else {
                            Node tmp = customer.getBag();
                            while (tmp.next != null) {
                                tmp = tmp.next;
                            }
                            tmp.next = node;
                        }
                    } else {
                        System.out.println("您的余额不足，请充值!");
                    }
                }
                case 2 : return ;
                default:
                    System.out.println("您输入的格式不正确，请重新输入");
            }
        }
    }

    public static class MerchantView {

        private final CommodityService commodityList =new CommodityService();
        public void enterMainMenu(){
            boolean ifFlag=true;
            while(ifFlag){
                System.out.println("----------------------商家界面-------------------------");
                System.out.println("                    1.添加商品");
                System.out.println("                    2.修改商品");
                System.out.println("                    3.删除商品");
                System.out.println("                    4.查看商品");
                System.out.println("                    5.退出\n");
                System.out.print("                 请选择（1-5）:");
                char menu= CMUtility.readMenuSelection();
                switch (menu) {
                    case '1' -> CommodityService.addCommodity(commodityList);
                    case '2' -> CommodityService.ModifyCommodity(commodityList);
                    case '3' -> CommodityService.deleteCommodity(commodityList);
                    case '4' -> CommodityService.printAllCommodity();
                    case '5' -> {
                        System.out.println("是否确认退出（Y/N）");
                        char isExit = CMUtility.readConfirmSelection();
                        if (isExit == 'Y') {
                            ifFlag = false;
                        }
                    }
                }
            }
        }
    }


    public static void main(String[] args) {
        View test = new View();
        test.enterMainMenu();
    }
}